package com.shop.controller;

import com.shop.client.UserFeignClient;
import com.shop.pojo.Article;
import com.shop.service.ArticleService;
import com.shop.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@SuppressWarnings("All")
@RestController
public class ArticleController {
    @Autowired
    public ArticleService articleService;

    @Autowired
    private UserFeignClient userFeignClient;

    @GetMapping("/getAllArticle")
    public Result getAllArticlei(){
        List<Article> list = articleService.findAllArticle();
        Result result = new Result();
        result.setCode(200);
        result.setFlag(true);
        result.setData(list);
        result.setMessage("查询文章成功");
        return result;
    }


    @PostMapping("/addArticle")
    public Result addArticle(Article article) {
        System.out.println(article + "----");
        int i = articleService.addSelective(article);
        System.out.println(userFeignClient);
        //同时使该作者数量加一
        Integer authorId = Integer.parseInt(article.getAuthorId());
        System.out.println("authorId" + authorId);
        Result result = userFeignClient.updateArticleNumber(authorId, 1);
        System.out.println(result);
        if (i > 0) {
            return result;
        } else {
            return result;
        }
    }

    @RequestMapping(value = "/hello")
    public String hello() {

        return "hello";
    }


}